package com.example.woke;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.http.HttpMethods;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.woke.data.Data_address;
import com.woke.method.MyApp;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class AddadressActivity extends Activity implements OnItemClickListener {

	private ListView mListView;
	private MyApp application;
	private ArrayList<Data_address> datalist = new ArrayList<Data_address> ();
	private View mRelstiv1;
	private Myadapter adapter;
	private View mBar;
	private int frome;
	private Intent intent;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate (savedInstanceState);
		setContentView (R.layout.activity_addadress);

		application = (MyApp) getApplication ();
		intview ();

	}

	private void intview() {
		intent = getIntent ();
		frome = intent.getIntExtra ("frome", 0);
		findViewById (R.id.addadress_image_back).setOnClickListener (new OnClickListener () {
			@Override
			public void onClick(View v) {
				finish ();
			}
		});
		findViewById (R.id.addadress_more).setOnClickListener (new OnClickListener () {
			@Override
			public void onClick(View v) {
				int moren = 0;
				if (datalist.size () == 0) {
					moren = 1;
				}
				Intent intent = new Intent (AddadressActivity.this, DddressmoreActivity.class);
				intent.putExtra ("moren", moren);
				startActivityForResult (intent, 114);
			}
		});
		mBar = findViewById (R.id.addadress_progress_bar);
		mListView = (ListView) findViewById (R.id.addadress_listview);
		mListView.setOnItemClickListener (this);
		mRelstiv1 = findViewById (R.id.adress_bg);
		adapter = new Myadapter ();
		mListView.setAdapter (adapter);
		getaddressdata (false);
	}

	private void getaddressdata(final boolean isnew) {
		AsyncHttpClient ahc = new AsyncHttpClient ();
		RequestParams params = new RequestParams ();
		params.put ("g", "api");
		params.put ("m", "user");
		params.put ("a", "address");
		if (application.getDatas_load () != null) {
			params.put ("userid", application.getDatas_load ().getId ());
		}
		ahc.get (HttpMethods.BASEURL, params, new JsonHttpResponseHandler () {

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess (statusCode, headers, response);
				Log.e ("response", "" + response);
				try {
					String flag = response.getString ("flag");
					String string = response.getString ("info");
					if (flag.equals ("success")) {
						if (isnew == true) {
							datalist.clear ();
						}
						JSONObject object = response.getJSONObject ("data");
						JSONArray array = object.getJSONArray ("data");
						for (int i = 0; i < array.length (); i++) {
							JSONObject jsonObject = array.getJSONObject (i);
							String createtime = jsonObject.getString ("createtime");
							String id = jsonObject.getString ("id");
							String is_defalut = jsonObject.getString ("is_defalut");
							String phone = jsonObject.getString ("phone");
							String address = jsonObject.getString ("address");
							String name = jsonObject.getString ("name");
							String userid = jsonObject.getString ("userid");
							String code = jsonObject.getString ("code");
							Data_address list = new Data_address (createtime, id, is_defalut, phone, address, name, userid, code);
							datalist.add (list);
						}
					} else {
						Toast.makeText (AddadressActivity.this, "数据获取失败！", Toast.LENGTH_SHORT)
								.show ();
					}
				} catch (JSONException e) {
					e.printStackTrace ();
				}
				if (datalist.size () > 0) {
					adapter.notifyDataSetChanged ();
					mRelstiv1.setBackgroundColor (getResources ().getColor (R.color.usercenterbg));
				}
				if (datalist.size () == 0) {
//					mRelstiv1.setBackgroundDrawable (getResources ().getDrawable (R.drawable.address_nodress));
					Toast.makeText(AddadressActivity.this,"暂无数据",Toast.LENGTH_SHORT).show();
				}

			}

			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart ();
				mBar.setVisibility (View.VISIBLE);
			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure (statusCode, headers, responseString, throwable);
				Toast.makeText (AddadressActivity.this, "退出失败！", Toast.LENGTH_SHORT).show ();
				Log.e ("response", "失败" + responseString);
			}

			@Override
			public void onFinish() {
				super.onFinish ();
				mBar.setVisibility (View.GONE);
			}
		});
	}

	class Myadapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return datalist.size ();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		class Holder {
			ImageView mImage_icon;
			TextView mText_adress;
			TextView mText_phone;
			TextView mText_name;
			View visde;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Holder holder = null;
			if (convertView == null) {
				holder = new Holder ();
				convertView = getLayoutInflater ().inflate (R.layout.item_adter_addresslist, null);
				holder.visde = convertView.findViewById (R.id.item_address_relate);
				holder.mImage_icon = (ImageView) convertView.findViewById (R.id.item_address_image);
				holder.mText_name = (TextView) convertView.findViewById (R.id.item_address_name);
				holder.mText_phone = (TextView) convertView.findViewById (R.id.item_address_phone);
				holder.mText_adress = (TextView) convertView.findViewById (R.id.item_address_adress);
				convertView.setTag (holder);
			} else {
				holder = (Holder) convertView.getTag ();
			}
			Data_address address = datalist.get (position);

			holder.mText_name.setText (address.name);
			holder.mText_phone.setText (address.phone);
			holder.mText_adress.setText (address.address);
			if (address.is_defalut.equals ("0")) {
				holder.mImage_icon.setVisibility (View.INVISIBLE);
				holder.visde.setBackgroundColor (getResources ().getColor (R.color.white));
			} else {
				holder.mImage_icon.setVisibility (View.VISIBLE);
				holder.visde.setBackgroundColor (getResources ().getColor (R.color.lsitchecke));
			}
			return convertView;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult (requestCode, resultCode, data);

		if (requestCode == 111 && resultCode == 112) {
			int intExtra = data.getIntExtra ("arg2", - 1);
			if (intExtra > - 1) {
				datalist.remove (intExtra);
				if (datalist.size () == 0) {
//					mRelstiv1.setBackgroundDrawable (getResources ().getDrawable (R.drawable.address_nodress));
					Toast.makeText(AddadressActivity.this,"暂无数据",Toast.LENGTH_SHORT).show();
				}
				adapter.notifyDataSetChanged ();
			} else {
				getaddressdata (true);
			}
		}
		if (requestCode == 114 && resultCode == 113) {
			getaddressdata (true);
		}

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Data_address data = datalist.get (arg2);
		if (frome == 0) {
			Intent intent = new Intent (AddadressActivity.this, AdressinfoActivity.class);
			intent.putExtra ("name", data.name);
			intent.putExtra ("address", data.address);
			intent.putExtra ("phone", data.phone);
			intent.putExtra ("code", data.code);
			intent.putExtra ("id", data.id);
			intent.putExtra ("arg2", arg2);
			intent.putExtra ("is_defalut", data.is_defalut);
			startActivityForResult (intent, 111);
		} else {
			ArrayList<String> list = new ArrayList<String> ();
			list.add (data.name);
			list.add (data.phone);
			list.add (data.address);
			list.add (data.code);
			intent.putExtra ("result", list);
			setResult (20, intent);
			finish ();
		}
	}
}
